4
4
.
.
3
3
.
.
5
5
T
T
e
e
s
s
t
t
-
-
R
R
e
e
q
q
u
u
e
e
s
s
t
t
-
-
B
B
o
o
d
d
y
y
-
-
J
J
S
S
O
O
N
N
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to Desterilize DTO and send it to Controller through JSON Body.
Application Schema [Result]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
Syntax
//CREATE REQUEST
MockHttpServletRequestBuilder request =
post ("/AddPerson") //POST
.contentType("application/json") //Type of Body Content
.content (personDTOSeriaized); //{ "name": "John", "age": 20 }
//PERFORM REQUEST. CHECK STATUS.
mockMvc.perform(request).andExpect(status().isOk());
MyController
PersonDTO
Postman
HTTP POST Request
http://localhost:8080/addPerson
{ "name": "John", "age": 20 }
MyControllerTest
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_test_mockmvc_jsonbody (add Spring Boot Starters from the table)
Edit File: pom.xml (add validation dependency)
Create Package: DTO (inside main package)
– Create Class: PersonDTO.java (inside package DTO)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Test Class: MyControllerTest.java
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
PersonDTO.java
package com.ivoronline.springboot_test_mockmvc_jsonbody.DTO;
import javax.validation.constraints.NotNull;
public class PersonDTO {
@NotNull public String name;
@NotNull public Integer age;
}
MyController.java
package com.ivoronline.springboot_test_mockmvc_jsonbody.controllers;
import com.ivoronline.springboot_test_mockmvc_jsonbody.DTO.PersonDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson(@Valid @RequestBody PersonDTO personDTO) {
//GET DATA FROM PersonDTO
String name = personDTO.name;
Integer age = personDTO.age;
//RETURN SOMETHING
return name + " is " + age + " years old";
}
}
MyControllerTest.java
package com.ivoronline.springboot_test_mockmvc_jsonbody.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ivoronline.springboot_test_mockmvc_jsonbody.DTO.PersonDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
class MyControllerTest {
@Autowired MockMvc mockMvc;
@Autowired MyController myController;
@Autowired ObjectMapper objectMapper;
@Test
void addPerson() throws Exception {
//CREATE PERSON DTO
PersonDTO personDTO = new PersonDTO();
personDTO.name = "John";
personDTO.age = 20;
//SERIALIZE PERSON DTO INTO JSON STRING { "name": "John", "age": 20 }
String personDTOSeriaized = objectMapper.writeValueAsString(personDTO);
//CREATE REQUEST
MockHttpServletRequestBuilder request =
post ("/AddPerson")
.contentType("application/json")
.content (personDTOSeriaized);
//PERFORM REQUEST
mockMvc.perform(request).andExpect(status().isOk());
}
}
R
R
e
e
s
s
u
u
l
l
t
t
Start Postman
POST
http://localhost:8080/addPerson
Headers (add Key-Value)
Content-Type: application/json
Body (option: raw)
{
"name" : "John",
"age" : 20
}
Postman
Missing Parameter personDTO.name = null
MockHttpServletRequest:
HTTP Method = POST
Request URI = /AddPerson
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"22"]
Body = {"name":null,"age":20}
MockHttpServletResponse:
Status = 400
Error message = null
java.lang.AssertionError: Status expected:<200> but was:<400>
Expected :200
Actual :400 Bad Request
Run Test Class: MyControllerTest.java
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>